from IPython.parallel import Client

client = Client(profile = 'rwth_physik')
#client = Client(profile = 'rwth_physik_tag')
dv = client.direct_view()

@dv.remote(block=True)
def getpid():
    import os
    return os.getcwd()

import datetime as dt
import shutil as sh
with dv.sync_imports():
    import os
    import subprocess

present = dt.datetime.now()
ymd = '_'.join(map(str, [present.year, present.month, present.day]))
wall_time = '_'+str(present.hour) + ':' + str(present.minute).rjust(2,'0') + ':' + str(present.second).rjust(2,'0')
timestamp = ymd + wall_time

#create directories to keep output organized
#high-level directory == timestamp

local_dir = '/mount/vol1/scratch/work/criger/repos/stochastic-master-equations/python/'
home_dir = '/net/home/lxtsfs1/tpa/criger/Documents' 
remote_dir = home_dir + '/temp_shared_data/' + timestamp
run_dir = home_dir + '/temp_shared_data/trajectory_run_dir'

#os.chdir(local_dir)
#os.makedirs(timestamp+'/pickled_output') 
#os.mkdir(timestamp+'/output_arrays') 
os.makedirs(remote_dir+'/pickled_output') 
os.mkdir(remote_dir+'/output_arrays') 

#copy in a bunch of files
#sh.copy('jobscript.sh',timestamp)
sh.copy(local_dir + 'get_trajectory.py', remote_dir)
sh.copy(local_dir + 'trajectory_input_file', remote_dir)

dv.block = True
dv.push({'remote_dir' : remote_dir})
dv.execute('os.chdir(remote_dir)')
num_runs = (1000/len(dv)+1)*len(dv)
#num_runs = len(dv)
dv.map_sync(lambda idx: subprocess.call(['/usr/bin/nice', '-n19', 'python', 'get_trajectory.py', str(idx), remote_dir]),range(1, num_runs + 1))
#dv.map_sync(lambda idx: subprocess.call(['python', 'get_trajectory.py', str(idx)]),range(1,1038))

